package com.mall.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mall.dw.model.HotGoods;
import com.mall.dw.util.DruidPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author Darren
 * @date 2022-10-14 9:45
 * Description：
 */
@Mapper
public interface HotGoodsMapper extends BaseMapper<HotGoods> {

    /**
     * 查询前N条记录
     *
     * @param size
     * @return
     */
    @Select("select uri, __time as accesstime, ip from mslogs limit #{size}")
    List<HotGoods> topNum(@Param("size") Integer size);

    /**
     * 分页查询
     *
     * @param size
     * @param offset
     * @return
     */
    @Select("select uri, __time as accesstime, ip from mslogs limit #{size} offset #{offset}")
    List<HotGoods> pageList(@Param("size") Integer size, @Param("offset") Long offset);

    /**
     * 分页查询 + 排序
     *
     * @param druidPage
     * @return
     */
    @Select("select uri, __time as accesstime, ip from mslogs order by ${druidPage.sort} ${druidPage.sortType} limit #{druidPage.size} offset #{druidPage.offset}")
    List<HotGoods> pageListSort(@Param("druidPage") DruidPage druidPage);

    /**
     * 根据时间查询,返回此时间内的数据
     *
     * @param size
     * @param time
     * @return
     */
    @Select("select uri, __time as accesstime, ip from mslogs where __time >= TIMESTAMP '${time}' limit #{size}")
    List<HotGoods> listByTime(@Param("size") Integer size, @Param("time") String time);

    /**
     * 根据时间查询,返回此时间内的数据 + 排除指定数据
     *
     * @param size
     * @param time
     * @param urls
     * @return
     */
    @Select("select uri, __time as accesstime, ip from mslogs where __time >= TIMESTAMP '${time}' and uri not in ('${urls}') limit #{size}")
    List<HotGoods> listByTimeAndExclude(@Param("size") Integer size, @Param("time") String time, @Param("urls") String urls);

    /**
     * 热门商品查询
     *
     * @param size
     * @param time
     * @param urls
     * @param max
     * @return
     */
    @Select("select uri,count(*) as viewCount from mslogs " +
            "WHERE __time >= TIMESTAMP '${time}' and uri NOT IN ('${urls}') " +
            "GROUP BY uri " +
            "HAVING viewCount > #{max} " +
            "ORDER BY viewCount DESC " +
            "limit #{size}")
    List<Map<String, String>> searchHotGoods(@Param("size") Integer size, @Param("time") String time,
                                             @Param("urls") String urls, @Param("max") Integer max);

}
